package com.woniuxy.redis01.controller;

import com.woniuxy.redis01.service.GoodService;
import com.woniuxy.redis01.service.SomeSerevice;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author author
 * @Create 2023-10-27 14:50
 */
@RestController
public class MyController {

    @Autowired
    SomeSerevice someSerevice;
    @Autowired
    GoodService goodService;

    @RequestMapping("test")
    public String test() {
        String str = someSerevice.test();
        return str;
    }

    @RequestMapping("incr")
    public String incr() {
        someSerevice.incr();
        return "incr ok";
    }

    @RequestMapping("init")
    public String init() {
        goodService.init();
        return "init ok";
    }

    @RequestMapping("second")
    public String second() {
        goodService.second();
        return "second ok";
    }

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("delayplugin")
    public String delayplugin() {
        System.out.println("发送到延迟队列，延迟处理:"+new Date());
        rabbitTemplate.convertSendAndReceive("exchangedelayplugin", "delayplugin", "delay-xxxx",
                //设置消息的附件属性
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        //设置消息头
                        message.getMessageProperties().setHeader("h1", "v1");
                        message.getMessageProperties().setHeader("h2", "v2");
                        //设置消息过期时间
//                        message.getMessageProperties().setExpiration("10000");
                        //设置消息延迟发送的时间
                        message.getMessageProperties().setDelay(10000);
                        return message;
                    }
                });
        return "delayplugin ok";
    }
}
